import Vue from 'vue';
import Vuex from 'vuex';
import { login } from '@/api/login';
import { queryConfigList } from '@/api/system/tbSysConfig';
import { queryPermissionMenu } from '@/api/system/tbSysMenu';
import { getEnumAndConst } from '@/api/system/tbSysDictDefinition';
import { siteBaseInfo, getAttachUrl, getDictData } from '@/api/openApi';
Vue.use(Vuex)

const state = {
	token: '',
	menuData: [],
	siteInfo: {},
	userInfo: {},
	dictEnums: {},
	dictConst: {},
	authButton: [],
	siteConfig: []
}

const actions = {
	init: ({ state, dispatch }) => {
		return new Promise(async (resolve, reject) => {
			await dispatch('getSiteBaseInfo');
			resolve();
		}).catch(() => {
			reject();
		})
	},

	//获取站点基本信息
	async getSiteBaseInfo({ commit, dispatch }) {
		let fileArr = ['field1', 'field8', 'field3', 'userPic', 'webLogo'];
		let result = await siteBaseInfo().then(res => res.data);
		if (result.code === '0') {
			let datas = !result.data ? {} : result.data[0];
			document.title = datas['name'];
			if (!!datas.field1) {
				datas['loginBgPath'] = await dispatch('getFileUrl', datas.field1);
			}
			if (!!datas.field8) {
				datas['loginLogoPath'] = await dispatch('getFileUrl', datas.field8);
			}
			if (!!datas.field3) {
				datas['defaultCover'] = await dispatch('getFileUrl', datas.field3);
			}
			if (!!datas.userPic) {
				datas['userPicPath'] = await dispatch('getFileUrl', datas.userPic);
			}
			if (!!datas.webLogo) {
				datas['webLogoPath'] = await dispatch('getFileUrl', datas.webLogo);
			}
			commit('SET_SITE_INFO', datas);
		}
	},

	//用户登录请求
	async userLogin({ commit }, params) {
		let result = await login(params);
		commit('SET_TOKEN', result.headers['authorization']);
		return Promise.resolve(result.data);
	},

	//获取权限菜单
	async getPermissionMenu({ commit }) {
		let result = await queryPermissionMenu().then(res => res.data);
		if (result.code === '0') {
			let datas = !result.data ? {} : result.data[0];
			let { menuToRouter } = require('@/utils/getRouter');
			commit('SET_MENU_DATA', new menuToRouter(datas.menu));
			commit('SET_AUTH_BUTTON', datas.button);
		}
	},

	//获取数据字典数据
	async getOpenDictData({ commit }) {
		let result = await getEnumAndConst().then(res => res.data);
		if (result.code === '0') {
			let datas = !result.data ? {} : result.data[0];
			commit('SET_DICT_DATA', datas);
		}
	},

	//获取附件地址
	async getFileUrl({ commit }, id) {
		let result = await getAttachUrl(id).then(res => res.data);
		if (result.code === '0') {
			let datas = Vue.prototype.$detailData(result.data);
			if (!Vue.prototype.$hasDomainInPath(datas)) {
				return `${window.staticPath}${datas}`
			} else {
				return datas;
			}
		} else {
			Promise.reject(new Error('faile'));
		}
	},

	//获取站点配置
	async getSiteConfig({ commit }) {
		let result = await queryConfigList().then(res => res.data);
		if (result.code === '0') {
			let datas = !result.data ? [] : result.data;
			commit('SET_SITE_CONFIG', datas);
		} else {
			Promise.reject(new Error('faile'));
		}
	}
}

const mutations = {
	SET_TOKEN: (state, d) => {
		state.token = d;
		Vue.prototype.$local.set('token', d);
	},
	SET_SITE_INFO: (state, d) => {
		state.siteInfo = d;
		Vue.prototype.$local.set('siteInfo', d);
	},
	SET_USER_INFO: (state, d) => {
		state.userInfo = d;
		Vue.prototype.$local.set('userInfo', d);
	},
	SET_MENU_DATA: (state, d) => {
		state.menuData = d;
		Vue.prototype.$local.set('menuList', d);
	},
	SET_AUTH_BUTTON: (state, d) => {
		state.authButton = d;
		Vue.prototype.$local.set('authButton', d);
	},
	SET_DICT_DATA: (state, d) => {
		state.dictEnums = d.enumKey;
		state.dictConst = d.constantKey;
		Vue.prototype.$local.set('dictEnums', d.enumKey);
		Vue.prototype.$local.set('dictConst', d.constantKey);
	},
	SET_SITE_CONFIG: (state, d) => {
		state.siteConfig = d;
		Vue.prototype.$local.set('siteConfig', d);
	}
}

export default { state, actions, mutations }